#!/usr/bin/env python
"""
黄淮学院教务系统快速启动脚本
"""

import os
import sys
import subprocess
import time

def check_mongodb():
    """检查MongoDB是否运行"""
    try:
        from pymongo import MongoClient
        client = MongoClient("mongodb://localhost:27017", serverSelectionTimeoutMS=2000)
        client.server_info()
        print("✅ MongoDB连接正常")
        return True
    except Exception as e:
        print(f"❌ MongoDB连接失败: {e}")
        print("请确保MongoDB服务正在运行")
        return False

def start_django():
    """启动Django服务器"""
    try:
        print("🚀 启动Django开发服务器...")
        subprocess.run([sys.executable, "manage.py", "runserver"], check=True)
    except KeyboardInterrupt:
        print("\n👋 服务器已停止")
    except Exception as e:
        print(f"❌ 启动失败: {e}")

if __name__ == "__main__":
    print("🎓 黄淮学院教务系统Django应用")
    print("=" * 40)
    
    # 检查MongoDB
    if not check_mongodb():
        print("\n请先启动MongoDB服务，然后重新运行此脚本")
        sys.exit(1)
    
    # 检查是否已运行迁移
    if not os.path.exists("db.sqlite3"):
        print("📦 运行Django迁移...")
        subprocess.run([sys.executable, "manage.py", "migrate"], check=True)
    
    print("\n🌐 服务器将在 http://127.0.0.1:8000/ 启动")
    print("按 Ctrl+C 停止服务器")
    print("=" * 40)
    
    # 启动Django
    start_django()
